from fetchemApp.models import *
import smtplib

def sendMarks (arg):
    if type(arg) == type(Submission.objects.all()[0]):
        if arg.Status == 'Graded' or 1==1:
            to = arg.student.Email
            gmail_user = arg.assignment.course.SubEmail
            gmail_pwd = arg.assignment.course.SubPassword
            sub =  arg.assignment.course.CourseNo + ' ' + arg.assignment.SubjectLine + ' Marks'
            body = 'Hi ' + arg.student.Name + ',\n\nHere is your evaluation for ' + arg.assignment.SubjectLine + ':\n'
            ######################
            q = arg.questiongrade_set.all()
            total = 0
            for x in q:
                QNO = str(x.question.QNo)
                t = x.question.ParentQuestion
                while t:
                    QNO = str(t.QNo) + '.' + QNO
                    t = t.ParentQuestion
                body += '\nQ ' + str(QNO) +' = ' + str(x.Marks)
                total += x.Marks
            body += '\n\nTotal Marks = ' + str(total) + '/' + str(arg.assignment.MaxMarks)
            body += '\nBonusType: ' + str(arg.BonusPenaltyType)
            body += '\nBonus Value: ' + str(arg.BonusPenaltyValue)
            body += '\n\nComments: ' + str(arg.Comment)
            body += '\n\nGraded by: ' + str(arg.grader.Name)
            body += "\n\nCheers,\nTeam Fetch'em"
            #######################
            smtpserver = smtplib.SMTP("smtp.gmail.com",587)
            smtpserver.ehlo()
            smtpserver.starttls()
            smtpserver.ehlo
            smtpserver.login(gmail_user, gmail_pwd)
            header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:' + sub + '\n'
            msg = header + '\n' + body
            smtpserver.sendmail(gmail_user, to, msg)
            arg.Status = 'Sent'
            arg.save()
            smtpserver.close()
    else:
        if type(arg) == type(Assignment.objects.all()[0]):
            gmail_user = arg.course.SubEmail
            gmail_pwd = arg.course.SubPassword
            sub = arg.course.CourseNo + ' ' + arg.SubjectLine + ' Marks'
            smtpserver = smtplib.SMTP("smtp.gmail.com",587)
            smtpserver.ehlo()
            smtpserver.starttls()
            smtpserver.ehlo
            smtpserver.login(gmail_user, gmail_pwd)
            subs = arg.submission_set.all()
            if not subs:
                studs = arg.course.Students.all()
                for stud in studs:
                    to = stud.Email
                    body = '\n' + stud.Name + '\n' + arg.SubjectLine + '\n\nTotal Marks = 0/' + arg.MaxMarks
                    header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:' + sub + '\n'
                    msg = header + '\n' + body
                    smtpserver.sendmail(gmail_user, to, msg)
            else:
                for sub in subs:
                    if sub.Status == 'Graded':
                        to = sub.student.Email
                        gmail_user = arg.course.SubEmail
                        gmail_pwd = arg.course.SubPassword
                        subj =  arg.course.CourseNo + ' ' + arg.SubjectLine + ' Marks'
                        body = 'Hi ' + sub.student.Name + ',\n\nHere is your evaluation for ' + arg.SubjectLine + ':\n'
                        ######################
                        q = sub.questiongrade_set.all()
                        total = 0
                        for x in q:
                            QNO = str(x.question.QNo)
                            t = x.question.ParentQuestion
                            while t:
                                QNO = str(t.QNo) + '.' + QNO
                                t = t.ParentQuestion
                            print QNO
                            body += '\nQ ' + str(QNO) +' = ' + str(x.Marks)
                            print body
                            total += x.Marks
                        body += '\n\nTotal Marks = ' + str(total) + '/' + str(arg.MaxMarks)
                        body += '\nBonusType: ' + str(sub.BonusPenaltyType)
                        body += '\nBonus Value: ' + str(sub.BonusPenaltyValue)
                        body += '\n\nComments: ' + str(sub.Comment)
                        body += '\n\nGraded by: ' + str(sub.grader.Name)
                        body += "\n\nCheers,\nTeam Fetch'em"
                        #######################
                        header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:' + subj + '\n'
                        msg = header + '\n' + body
                        smtpserver.sendmail(gmail_user, to, msg)
                        sub.Status = 'Sent'
                        sub.save()
                ##############
                studs = []
                for i in arg.course.Students.all():
                    if not Submission.objects.filter(assignment=arg,student=i):
                        studs+= [i]
                ##############
                for stud in studs:
                    to = stud.Email
                    body = '\n' + stud.Name + '\n' + arg.SubjectLine + '\n\nTotal Marks = 0/' + arg.MaxMarks
                    header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:' + sub + '\n'
                    msg = header + '\n' + body
                    smtpserver.sendmail(gmail_user, to, msg)
            smtpserver.close()